<?php
/**
 * @Author:      dx
 * @DateTime:    2016-2-16 15:32:48
 * @Description: 用于 mkt 系统用户的登录登出
 */
class MktUserController extends BaseController {
    /*
     * 市场活动人员 登陆
     * */
    public function login(){
        $validator = Validator::make(Input::all(),
            array(
                'username' => 'required',
                'passwd' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("mkt_error.login_args_error_210001");
                break;
            }

            //传入参数
            $username = Input::get('username');
            $passwd = MktUser::make(Input::get('passwd'));

            //验证密码
            $mkt_user=MktUser::findUserByUP($username,$passwd);
                
            if(!is_null($mkt_user)){
                if($mkt_user->status!=1){
                    $response = Lang::get("mkt_error.login_user_status_error_210002");
                    break;
                }

                //删除 mkt 用户 token
                try {
                    MktUserToken::where('mkt_user_id',$mkt_user->id)->delete();
                    $mkt_token = new MktUserToken;
                    $mkt_token->mkt_user_id = $mkt_user->id;
                    $mkt_token->token = md5($username.time().rand(0,100));
                    $mkt_token->created_at = Common::num2DateTime(time());
                    $mkt_token->out_time_at = Common::num2DateTime(time() + Config::get("scs_user.token_time_out"));
                    $mkt_token->save();
                } catch (Exception $e) {
                    Log::error("error", array("system" => "scs" ,"event" => "login", 'input' =>Input::all() ,"exception" => $e));
                    $response = Lang::get("mkt_error.login_create_token_error_210004");
                    break;
                }
            }else{
                $response = Lang::get("mkt_error.login_user_userpw_error_210003");
                break;
            }

            //组织返回
            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => array(
                    'name' => $mkt_user->real_name,
                    'token' => $mkt_token->token,
                    'expired' => $mkt_token->out_time_at
                )
            );
        } while (false);
        Log::info("info", array("system" => "mkt" ,"event" => "login", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      dx
     * @DateTime:    2016-2-16 19:04:14
     * @Description: mkt 用户登出
     */
    public function logout(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("mkt_error.login_token_error_210005");
                break;
            }
            //传入参数
            $response = array('code' => 0, 'msg' => 'ok');
            $mkt_user=$this->verificationMKTUserToken(Input::get("token"),$response);
            if(is_null($mkt_user)){
                break;
            }
            try{
                MktUserToken::where('mkt_user_id',$mkt_user->id)->delete();
            } catch (Exception $e) {
                Log::error("error", array("system" => "mkt" ,"event" => "logout", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("mkt_error.user_token_clean_error_210007");
                break;
            }
            //组织返回
            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => array()
            );
        } while (false);
        Log::info("info", array("system" => "mkt" ,"event" => "logout", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


}